<?php

namespace app\common\model;

use think\Exception;
use think\Request;
use think\Facade\Session;

class Admin extends BaseModel
{

    public function setPasswordAttr($value){
        return password($value);
    }

    /**
     * 登录处理
     * @param Request $request
     * @return bool
     */
    public static function login($username,$password){
        if(!$username || !$password){
            throw new Exception("用户名或密码错误！");
        }
        $admin_info=self::where('username',$username)->find();

        if(empty($admin_info)){
            throw new Exception("用户名或密码错误！");
        }

        if(password($password) === $admin_info['password']){
            Session::set("admin_id",$admin_info['id']);
            Session::set("admin_info",$admin_info);
            return true;
        }
        return false;
    }
}
